<template>
  <div class="template global-container">
    <div class="title">全局</div>
    <div class="content">
      <div class="block">
        <div class="dt">背景</div>
        <div class="dd">
          <div class="background">
            <upload @on-success="imgUpdateSuccessHandle">
              <el-image v-if="background.url" :src="background.url" fit="contain" />
              <el-button v-else class="button-upload" icon="el-icon-plus" plain></el-button>
              <span class="delete" v-if="background.url" @click.stop="removeBackgroundHandle">
                <i class="el-icon-delete"></i>
              </span>
            </upload>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Upload from '../../../upload'
import { mapState } from 'vuex'
import { STORE_NAMESPACE } from '../../../../config'
export default {
  name: 'PanelGlobal',
  components: {
    Upload
  },
  data () {
    return {}
  },
  computed: {
    ...mapState({
      background: state => state[STORE_NAMESPACE].background || {}
    })
  },
  methods: {
    imgUpdateSuccessHandle (file) {
      this.$store.dispatch(`${STORE_NAMESPACE}/setBackground`, {
        url: file,
        uuid: ''
      })
    },
    removeBackgroundHandle () {
      this.$store.dispatch(`${STORE_NAMESPACE}/setBackground`, {
        url: '',
        uuid: ''
      })
    }
  }
}
</script>

<style lang="less" scoped>
.global-container {
  .dd {
    .background {
      overflow: hidden;
      position: relative;
      width: 100px;
      height: 100px;
      border-radius: 5px;
      ::v-deep .el-upload,
      & .el-button {
        width: 100%;
        height: 100%;
      }
      .delete {
        position: absolute;
        left: 0;
        top: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        text-align: center;
        color: #fff;
        font-size: 20px;
        background-color: rgba(0,0,0,.5);
        transition: opacity .3s;
        opacity: 0;
        &:hover {
          opacity: 1;
        }
        i {
          cursor: pointer;
        }
      }
    }
  }
}
</style>